<template>

	<view class="my-settle-container">
		<!-- 全选 -->
		<label class="radio" @click="checkedAll">
			<radio :checked="ischeckedAll" color="#c00000" /><text>全选</text>
		</label>
		<!-- 合计 -->
		<view class="amount-box">
			合计:<text class="amount">￥{{totalPrice}}</text>
		</view>
		<!-- 结算按钮 -->
		<view class="btn-settle" @click="settlement">
			结算({{checkedCount}})
		</view>
	</view>


</template>

<script>
	import {
		mapState,
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		name: "my-settle",
		data() {
			return {
				seconds: 3,
				timer: null
			};
		},
		computed: {
			...mapState('m_user', ['token']),
			...mapGetters('m_user', ['addstr']),
			...mapGetters('m_cart', ['checkedCount', 'totalPrice', 'total']),
			// 判断商品总数是否等于选中商品总数,即判断所有商品是否选中,从而全选按钮选中
			ischeckedAll() {
				return this.total === this.checkedCount;
			}
		},
		methods: {
			...mapMutations('m_cart', ['checkedAllState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			checkedAll() {
				console.log(this.ischeckedAll);
				this.checkedAllState(!this.ischeckedAll)
			},
			// 点击了结算按钮
			settlement() {
				// 先判断是否勾选了商品
				if (!this.checkedCount) return uni.$showMsg('请选择要勾选的商品！')
				// 然后判断是否选择了收获地址
				if (!this.addstr) return uni.$showMsg('请选择收获地址！')
				// 最后判断用户是否登录
				if (!this.token) return this.delayNavigate(this.seconds)
			},
			// 显示倒计时提示
			showTip(n) {
				uni.showToast({
					// 图标
					icon: 'none',
					title: '请登录后再结算！' + n + '秒后自动跳转到登录页',
					// 遮罩
					mask: true,
					// 消失时间
					duration: 1500
				})
			},

			delayNavigate(n) {
				this.showTip(this.seconds)
				this.timer = setInterval(() => {
					this.seconds--;
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/my/my',
							// 成功的回调
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
						this.seconds = n
						return
					}
					this.showTip(this.seconds)
				}, 1000)

			}
		}

	}
</script>

<style lang="scss">
	.my-settle-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 999;
		font-size: 14px;
		padding-left: 15px;
		height: 50px;
		background-color: white;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount-box {
			.amount {
				color: #c00000;
				font-weight: 700;
			}
		}

		.btn-settle {
			height: 50px;
			line-height: 50px;
			color: white;
			padding: 0 10px;
			background-color: #c00000;
			min-width: 100px;
			text-align: center;
		}
	}
</style>
